package com.example.openai.model;


import com.alibaba.fastjson.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Data
@NoArgsConstructor
public class LoginUser implements UserDetails {
    private User user;
    private List<String> userPerm=new ArrayList<>();
    @JSONField(serialize = false,deserialize = false)
    private transient List<SimpleGrantedAuthority> items=new ArrayList<>();
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public List<String> getUserPerm() {
        return userPerm;
    }

    public void setUserPerm(List<String> userPerm) {
        this.userPerm = userPerm;
    }

    public LoginUser(User user, List<String> userPerm) {
        this.user = user;
        this.userPerm = userPerm;
    }


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if(items.size()!=0){
            return items;
        }
        items=new ArrayList<>();
        for (String item : userPerm) {
            SimpleGrantedAuthority grantedAuthority=new SimpleGrantedAuthority(item);
            items.add(grantedAuthority);
        }
        return items;
    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUserName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
